package cn.iocoder.yudao.module.flgeo.controller.admin.geoshp;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.flgeo.controller.admin.geoshp.vo.*;
import cn.iocoder.yudao.module.flgeo.dal.dataobject.geoshp.GeoshpDO;
import cn.iocoder.yudao.module.flgeo.convert.geoshp.GeoshpConvert;
import cn.iocoder.yudao.module.flgeo.service.geoshp.GeoshpService;

@Tag(name = "管理后台 - 图形")
@RestController
@RequestMapping("/flgeo/geoshp")
@Validated
public class GeoshpController {

    @Resource
    private GeoshpService geoshpService;

    @PostMapping("/create")
    @Operation(summary = "创建图形")
    @PreAuthorize("@ss.hasPermission('flgeo:geoshp:create')")
    public CommonResult<Long> createGeoshp(@Valid @RequestBody GeoshpCreateReqVO createReqVO) {
        return success(geoshpService.createGeoshp(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新图形")
    @PreAuthorize("@ss.hasPermission('flgeo:geoshp:update')")
    public CommonResult<Boolean> updateGeoshp(@Valid @RequestBody GeoshpUpdateReqVO updateReqVO) {
        geoshpService.updateGeoshp(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除图形")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('flgeo:geoshp:delete')")
    public CommonResult<Boolean> deleteGeoshp(@RequestParam("id") Long id) {
        geoshpService.deleteGeoshp(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得图形")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('flgeo:geoshp:query')")
    public CommonResult<GeoshpRespVO> getGeoshp(@RequestParam("id") Long id) {
        GeoshpDO geoshp = geoshpService.getGeoshp(id);
        return success(GeoshpConvert.INSTANCE.convert(geoshp));
    }

    @GetMapping("/list")
    @Operation(summary = "获得图形列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('flgeo:geoshp:query')")
    public CommonResult<List<GeoshpRespVO>> getGeoshpList(@RequestParam("ids") Collection<Long> ids) {
        List<GeoshpDO> list = geoshpService.getGeoshpList(ids);
        return success(GeoshpConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得图形分页")
    @PreAuthorize("@ss.hasPermission('flgeo:geoshp:query')")
    public CommonResult<PageResult<GeoshpRespVO>> getGeoshpPage(@Valid GeoshpPageReqVO pageVO) {
        PageResult<GeoshpDO> pageResult = geoshpService.getGeoshpPage(pageVO);
        return success(GeoshpConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出图形 Excel")
    @PreAuthorize("@ss.hasPermission('flgeo:geoshp:export')")
    @OperateLog(type = EXPORT)
    public void exportGeoshpExcel(@Valid GeoshpExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<GeoshpDO> list = geoshpService.getGeoshpList(exportReqVO);
        // 导出 Excel
        List<GeoshpExcelVO> datas = GeoshpConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "图形.xls", "数据", GeoshpExcelVO.class, datas);
    }

}
